package jt.classic.system.workflow;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.Column;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.Table;
import org.limp.basework.analyzer.TableRegisterCenter;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DataTrimmer;
import org.limp.mine.DateTrimmer;
import org.limp.mine.StringTool;
import org.limp.mine.annex.AnnexInfo;

import java.sql.Connection;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 流转action
 *
 * @author bizkit
 *
 */
public class WorkflowAction extends SimpleAction {
    /**
     * 下一步环节办理人选择
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward trenduser(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        /*当前环节ID*/
        String id = StringTool.checkString(request.getParameter("sid"));

        /*活动记录ID*/
        String aid = StringTool.checkString(request.getParameter("aid"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowManager manager = new WorkflowManager(conn);

            List trends = manager.getTrendUser(aid, id, user);

            JSONArray jsonArray = JSONArray.fromObject(trends);

            request.setAttribute("result", jsonArray.toString());
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 特送选择下一步环节办理人选择
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward sdtrenduser(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        /*当前环节ID*/
        String id = StringTool.checkString(request.getParameter("sid"));

        /*活动记录ID*/
        String aid = StringTool.checkString(request.getParameter("aid"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowManager manager = new WorkflowManager(conn);

            WInstanceManager wimanager = new WInstanceManager(conn);

            WActivity act = wimanager.getActivityByID(aid);

            WStep step = act.instance().iflow().istep(id);

            List steps = act.instance().iflow().isteps();

            List resultList = new ArrayList();

            if ((steps != null) && !steps.isEmpty()) {
                for (int i = 0; i < steps.size(); i++) {
                    WStep nextStep = (WStep) steps.get(i);

                    if (!nextStep.id().equals(id) && (nextStep.itype() != -1) && (step.inner() == nextStep.inner())) {
                        List trends = manager.getSDTrendUser(aid, step,
                                nextStep, user);

                        resultList.addAll(trends);
                    }
                }
            }

            JSONArray jsonArray = JSONArray.fromObject(resultList);

            request.setAttribute("result", jsonArray.toString());
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
    * 新建流程对象
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return
    * @throws Exception
    */
    public ActionForward neu(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        SimpleBean sb = new CommonBean4HSQ(request);
        Map param = sb.getResource();

        /*信鸽参数*/
        request.setAttribute("pigeonparameter", param);

        String key = StringTool.checkString(param.get("key"));

        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);
            WFlow flow = engine.nev(key, user);

            List pluglist = flow.istep4start().iplug4intro();

            if ((pluglist != null) && !pluglist.isEmpty()) {
                for (int i = 0; i < pluglist.size(); i++) {
                    String className = (String) pluglist.get(i);

                    WPlugHandler handler = (WPlugHandler) Class.forName(className)
                                                               .newInstance();
                    Object result = handler.participate(conn, sb);

                    /*将前置插件处理结果传递到起草表单页面*/
                    request.setAttribute(className, result);
                }
            }

            WObject obj = flow.iobject();

            request.setAttribute("action", "neu");
            request.setAttribute("flow", flow);
            request.setAttribute("step", flow.istep4start());
            request.setAttribute("readonly", "false");
            request.setAttribute("flowtype", "custom");

            WorkflowManager manager = new WorkflowManager(conn);

            List printDocTempList = manager.getDocTemp(3, flow.id());

            request.setAttribute("printDocTempList", printDocTempList);

            List list = manager.getNumberType("GROUP", obj.ikey());
            Map defaultnum = manager.getDefaultNumber4User(user, obj.ikey());
            Map doctemp = manager.getDocTempInUsed(flow.id());

            request.setAttribute("doctemp", doctemp);
            request.setAttribute("numbers", list);
            request.setAttribute("defaultnumber", defaultnum);

            if (obj.iview4diy()) {
                forward = mapping.findForward("neu4diy");
            } else {
                forward = new ActionForward();
                forward.setPath(obj.iview4new());
            }

            conn.commit();
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 保存
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            String aid = engine.save(request);

            conn.commit();

            request.setAttribute("result",
                "{success: true, result : " + aid + "}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            conn.rollback();
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 编辑
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            WActivity activity = engine.edit(id, user);

            request.setAttribute("action", "edit");
            request.setAttribute("activity", activity);
            request.setAttribute("flow", activity.instance().iflow());
            request.setAttribute("step", activity.istep());

            if (activity.istate() == 2) {
                request.setAttribute("readonly", "true");
            } else {
                request.setAttribute("readonly", "false");
            }

            WorkflowManager manager = new WorkflowManager(conn);

            List printDocTempList = manager.getDocTemp(3,
                    activity.instance().iflow().id());

            request.setAttribute("printDocTempList", printDocTempList);

            Map doctemp = manager.getDocTempInUsed(activity.instance().iflow()
                                                           .id());
            request.setAttribute("doctemp", doctemp);

            List list = manager.getNumberType("GROUP",
                    activity.instance().iflow().iobject().ikey());
            request.setAttribute("numbers", list);

            Map defaultnum = manager.getDefaultNumber4User(user,
                    activity.instance().iflow().iobject().ikey());
            request.setAttribute("defaultnumber", defaultnum);

            WObject obj = activity.instance().iflow().iobject();

            if (obj.iview4diy()) {
                forward = mapping.findForward("neu4diy");
            } else {
                forward = new ActionForward();
                forward.setPath(obj.iview4new());
            }
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 打开流转活动记录
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward open(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            WActivity activity = engine.edit(id, user);

            String url = null;

            /*办理完成,查看*/
            if (activity.istate() == 2) {
                url = "/flow.do?method=view&id=" + id;
            }
            /*未完成,办理*/
            else {
                url = "/flow.do?method=edit&id=" + id;
            }

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 修改
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward update(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            String aid = engine.update(request);

            conn.commit();

            request.setAttribute("result",
                "{success: true, result : " + aid + "}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            conn.rollback();
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 删除
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        /*instance id*/
        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WInstanceManager wim = new WInstanceManager(conn);

            wim.deleteInstance(id, user);

            conn.commit();

            request.setAttribute("result", "{success: true");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            conn.rollback();
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 归档
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward pigeonhole(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        /*instance id*/
        String id = StringTool.checkString(request.getParameter("insid"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            BaseworkUtil util = new BaseworkUtil();

            SimpleBean4Table sbt = new SimpleBean4TableImpl("WF_PH", cbh);

            util.manualSave1(sbt, conn);

            WInstanceManager wim = new WInstanceManager(conn);

            wim.pigeonholeInstance(id, user);

            conn.commit();

            request.setAttribute("result", "{success: true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            conn.rollback();
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 撤销
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward cancel(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        /*instance id*/
        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            engine.cancel(id, user);

            conn.commit();

            request.setAttribute("result", "{success: true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            conn.rollback();
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 重新启用实例
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward awake(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        /*activity id*/
        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            engine.awake(id, user);

            conn.commit();

            request.setAttribute("result", "{success: true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            conn.rollback();
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 预览
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward view(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            WActivity activity = engine.view(id, user);

            request.setAttribute("action", "view");
            request.setAttribute("activity", activity);
            request.setAttribute("flow", activity.instance().iflow());
            request.setAttribute("step", activity.istep());
            request.setAttribute("readonly", "true");

            WorkflowManager manager = new WorkflowManager(conn);
            List list = manager.getNumberType("GROUP",
                    activity.instance().iflow().iobject().ikey());
            request.setAttribute("numbers", list);

            List printDocTempList = manager.getDocTemp(3,
                    activity.instance().iflow().id());

            request.setAttribute("printDocTempList", printDocTempList);

            WObject obj = activity.instance().iflow().iobject();

            if (obj.iview4diy()) {
                forward = mapping.findForward("neu4diy");
            } else {
                forward = new ActionForward();
                forward.setPath(obj.iview4new());
            }
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 预览传阅文件
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward viewencyclic(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            WActivity activity = engine.viewencyclic(id, user);

            SimpleBean4Table sbt = new SimpleBean4TableImpl("WF_READTRACE");
            sbt.getResource().put("aid", activity.id());
            sbt.getResource().put("uid", user.id());
            sbt.getResource().put("uname", user.iname());
            sbt.getResource().put("rdate", DateTrimmer.getYMDHMS());

            BaseworkUtil util = new BaseworkUtil();
            util.manualSave1(sbt, conn);

            conn.commit();

            request.setAttribute("action", "viewencyclic");
            request.setAttribute("activity", activity);
            request.setAttribute("flow", activity.instance().iflow());
            request.setAttribute("step", activity.istep());
            request.setAttribute("readonly", "true");

            WorkflowManager manager = new WorkflowManager(conn);
            List list = manager.getNumberType("GROUP",
                    activity.instance().iflow().iobject().ikey());
            request.setAttribute("numbers", list);

            List printDocTempList = manager.getDocTemp(3,
                    activity.instance().iflow().id());

            request.setAttribute("printDocTempList", printDocTempList);

            WObject obj = activity.instance().iflow().iobject();

            if (obj.iview4diy()) {
                forward = mapping.findForward("neu4diy");
            } else {
                forward = new ActionForward();
                forward.setPath(obj.iview4new());
            }
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 预览
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward lookover(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        //IUser user = ISystem.getSessionUser(request);
        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowManager manager = new WorkflowManager(conn);
            WInstanceManager imanager = new WInstanceManager(conn);
            WActivity activity = imanager.getActivityByID(id);

            request.setAttribute("action", "lookover");
            request.setAttribute("activity", activity);
            request.setAttribute("flow", activity.instance().iflow());
            request.setAttribute("step", activity.istep());
            request.setAttribute("readonly", "true");

            List list = manager.getNumberType("GROUP",
                    activity.instance().iflow().iobject().ikey());

            request.setAttribute("numbers", list);

            WObject obj = activity.instance().iflow().iobject();

            List printDocTempList = manager.getDocTemp(3,
                    activity.instance().iflow().id());

            request.setAttribute("printDocTempList", printDocTempList);

            if (obj.iview4diy()) {
                forward = mapping.findForward("neu4diy");
            } else {
                forward = new ActionForward();
                forward.setPath(obj.iview4new());
            }
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 传阅查看
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward encyclic(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            engine.encyclic(id, user);

            conn.commit();

            WActivity activity = engine.view(id, user);

            request.setAttribute("activity", activity);
            request.setAttribute("flow", activity.instance().iflow());
            request.setAttribute("step", activity.istep());
            request.setAttribute("readonly", "true");

            WObject obj = activity.instance().iflow().iobject();

            forward = new ActionForward();
            forward.setPath(obj.iview4view());
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 发送
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward send(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            engine.send(request);

            conn.commit();

            request.setAttribute("result", "{success: true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 工作流程过程
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward trace(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WInstanceManager imanager = new WInstanceManager(conn);

            WActivity activity = imanager.getActivityByID(id);

            request.setAttribute("activity", activity);

            forward = mapping.findForward("trace");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 流程记录取回
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward retake(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String cid = StringTool.checkString(request.getParameter("cid"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WInstanceManager imanager = new WInstanceManager(conn);
            WActivity activity = imanager.getActivityByID(id);

            WActivity crruent = activity.instance().iactivity(cid);

            IUser user = ISystem.getSessionUser(request);

            WorkflowEngine engine = new WorkflowEngine(conn);
            engine.retakeactivity(activity, crruent, user);

            conn.commit();

            request.setAttribute("result",
                "{success: true, result : '" +
                activity.instance().iflow().iobject().ikey() + "'}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 取回已归档公文
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward retakeF(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String key = StringTool.checkString(request.getParameter("key"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WInstanceManager imanager = new WInstanceManager(conn);
            WActivity activity = imanager.getActivityByID(id);

            WorkflowEngine engine = new WorkflowEngine(conn);
            engine.retakefactivity(activity);

            conn.commit();

            String url = "/flow.do?method=pigeonholelist&key=" + key;

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 流程记录退回
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward back(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);
            engine.backactivity(request);

            conn.commit();

            request.setAttribute("result", "{success: true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 保存实例正文
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward savedoc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;
        Connection conn = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);
            AnnexInfo annex = engine.savedoc(request);

            conn.commit();

            request.setAttribute("result",
                "{success: true, id : '" + annex.getID() + "'}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success: false}");

            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 获取实例流水号
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward getInstanceNumber(ActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Connection conn = null;

        String size = StringTool.checkString(request.getParameter("size"), "6");
        String sequence = StringTool.checkString(request.getParameter(
                    "sequence"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);
            String number = engine.getInstanceNumber(sequence,
                    Integer.parseInt(size));

            request.setAttribute("result", number);
        } catch (Exception ex) {
            request.setAttribute("result", "-1");

            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 获取流水号
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward getnumber(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String sequence = StringTool.checkString(request.getParameter(
                    "sequence"));
        String numberid = StringTool.checkString(request.getParameter(
                    "numberid"));

        String prefix = StringTool.checkString(request.getParameter("prefix"));

        String suffix = StringTool.checkString(request.getParameter("suffix"));

        String size = StringTool.checkString(request.getParameter("size"), "6");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);
            String number = engine.getnumber(sequence, numberid, prefix,
                    suffix, Integer.parseInt(size));

            conn.commit();

            request.setAttribute("result", number);
        } catch (Exception ex) {
            request.setAttribute("result", "-1");

            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 流水号校验
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward checknumber(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String numberid = StringTool.checkString(request.getParameter(
                    "numberid"));
        String prefix = StringTool.checkString(request.getParameter("prefix"));
        String number = StringTool.checkString(request.getParameter("number"));
        String sequence = StringTool.checkString(request.getParameter(
                    "sequence"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            /*文号是否被占用*/
            if (engine.checknumber(numberid, prefix, number, sequence)) {
                request.setAttribute("result", "-1");
            } else {
                request.setAttribute("result", "1");
            }

            conn.commit();
        } catch (Exception ex) {
            request.setAttribute("result", "-1");

            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 使用默认文号
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward repaynumber(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String numberid = request.getParameter("numberid");
        String number = request.getParameter("number");
        String sequence = request.getParameter("sequence");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            /*使用默认文号*/
            if (!engine.repaynumber(numberid, number, sequence)) {
                request.setAttribute("result", "-1");
            } else {
                request.setAttribute("result", "1");
            }

            conn.commit();
        } catch (Exception ex) {
            request.setAttribute("result", "-1");

            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 锁定文号
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward locknumber(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String numberid = request.getParameter("numberid");
        String number = request.getParameter("number");
        String sequence = request.getParameter("sequence");
        String prefix = request.getParameter("prefix");
        String suffix = request.getParameter("suffix");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);

            engine.locknumber(numberid, number, sequence, prefix, suffix);

            request.setAttribute("result", "1");

            conn.commit();
        } catch (Exception ex) {
            request.setAttribute("result", "-1");

            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 回收流水号
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward callbacknumber(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String sequence = StringTool.checkString(request.getParameter(
                    "sequence"));

        String number = StringTool.checkString(request.getParameter("number"));

        String numberid = StringTool.checkString(request.getParameter(
                    "numberid"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowEngine engine = new WorkflowEngine(conn);
            engine.callbacknumber(numberid, number, sequence);

            conn.commit();

            request.setAttribute("result", "1");
        } catch (Exception ex) {
            request.setAttribute("result", "-1");

            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 流程定制
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward tailor(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String key = StringTool.checkString(request.getParameter("key"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowManager manager = new WorkflowManager(conn);
            WFlow flow = manager.getWFlowByKey(key);

            request.setAttribute("flow", flow);

            forward = mapping.findForward("tailor");
        } catch (Exception ex) {
            request.setAttribute("result", "-1");
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 流程定制选择接收人
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward dotailor(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String stepid = StringTool.checkString(request.getParameter("stepid"));

        String tp = StringTool.checkString(request.getParameter("sel_" +
                    stepid));
        WStep step = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            WorkflowManager manager = new WorkflowManager(conn);

            step = manager.getStepByID(stepid);
        } catch (Exception ex) {
            request.setAttribute("result", "-1");
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        String type = (step.ipara() == 0) ? "radio" : "checkbox";

        if (tp.equals("default")) {
            IUser user = ISystem.getSessionUser(request);

            if ((step.iscorr() == 3)) {
                String url = "/tree.do?method=selectuseronly&type=" + type;

                forward = new ActionForward();
                forward.setPath(url);
                forward.setRedirect(true);
            } else {
                WSupport support = step.iflow().iobject().isupport();

                WSupportUGCenter supportCenter = support.iusergroupcenter();
                List list = null;

                try {
                    list = null; //supportCenter.getUserInChangelessStep(step, user);
                } catch (Exception ex) {
                    ISystem.catchException(this, ex);
                } finally {
                    supportCenter.clean();
                }

                String script = null;

                request.setAttribute("checkFolder", "false");
                request.setAttribute("checkFile", "true");
                request.setAttribute("type", type);
                request.setAttribute("script", script);

                forward = mapping.findForward("trenduser");
            }
        } else if (tp.equals("tailor")) {
            String url = "/tree.do?method=selectuseronly&type=" + type;

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        }

        return forward;
    }

    /**
     * web服务
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward deleteitem(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        String id = StringTool.checkString(request.getParameter("id"));

        String tableName = StringTool.checkString(request.getParameter(
                    "tableName"));

        Connection conn = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            TableRegisterCenter center = TableRegisterCenter.getInstance();
            Table table = center.findTable(tableName);
            Column column = table.column(table.getPrimaryKey()[0]);

            String sql = "DELETE FROM " + tableName + " WHERE " +
                column.getName() + " = '" + id + "'";

            DataTrimmer trimmer = new DataTrimmer(conn);

            if (trimmer.execute(sql)) {
                sql = "DELETE FROM WF_ACTIVITY_OBJ WHERE TABLENAME = '" +
                    tableName + "' AND KEYVALUE = '" + id + "'";

                trimmer.execute(sql);
            }

            conn.commit();

            request.setAttribute("result", "0");
        } catch (Exception ex) {
            request.setAttribute("result", "-1");
            conn.rollback();

            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * web服务
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward service(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        System.out.println(request.getAttribute("content"));

        return forward;
    }
}
